from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.select import Select
from time import strftime
class Base():
    # 初始化驱动
    def __init__(self, driver):
        self.driver=driver


    def screen_shot(self):
        nowtime=strftime('%Y%M%D%H%M%S')
        return self.driver.save_screenshot(nowtime+'error.png')


    def open_url(self,url):
        self.driver.get(url)

    def find_element(self,loc):
        try:
            WebDriverWait(self.driver,20, 0.5).until(lambda driver: self.driver.find_element(*loc).is_displayed)
            return self.driver.find_element(*loc)
        except Exception as e:
            self.screen_shot()
            print(e)


    def send_keys(self,loc,text):
        self.find_element(loc).send_keys(text)


    def click(self, loc):
        self.find_element(loc).click()

    def ctrl_(self,loc,word):
        self.send_keys(loc,Keys.CONTROL+word)

    def move_element(self,loc):
        ActionChains(self.driver).move_to_element(self.find_element(loc)).perform()

    def Select_by_index(self,loc,index):
        Select(self.find_element(loc)).select_by_index(index)

    def double_c(self,loc):
        ActionChains(self.driver).double_click(self.find_element(loc)).perform()